import sys,math
from PyQt5.QtCore import Qt, QPointF
from PyQt5.QtGui import QPainter
from PyQt5.QtWidgets import QApplication, QMainWindow
from  PyQt5.QtChart import QChartView, QChart, QScatterSeries,QLineSeries
data="""YSBS20255b-2009-1  0.155   0.1536  0.162725172
YSBS20255b-2009-2  0.43    0.4327  0.458406131
YSBS20255b-2009-3  0.67    0.6538  0.692641388
YSBS20255b-2009-5  0.0028  0.0027  0.002860403
YSBS20255b-2009-6  0.142   0.1394  0.147681568
YSBS20255b-2009-7  0.664   0.6183  0.65503238
GSB03-2453-2008-2  0.256   0.2409  0.255211548
GSB03-2453-2008-4  0.085   0.0849  0.089943796
YSBS11279-2000  0.803   0.7395  0.783432711
9902-GBW(E)010137   1.21    1.1099  1.175837682
Q235-YSBS11173a-2007    0.188   0.1942  0.205737164
16MnV5-YSBC35201-97-211 0.171   0.1646  0.174378667
Q345b-YSBS11264a-2011   0.158   0.1593  0.168763801
YSBS11078d-2012    0.0018  0.0016  0.001695054
BS1020 0.21    0.2138  0.226501573
GCr15-YSBS11273b-2007   0.999   0.962   1.01915114"""
data=data.split("\n")
for i in range(len(data)):
    data[i]=data[i].split(" ")
    data[i] = list(filter(lambda x: x!="", data[i]))
std=[]
measure=[]
yfited=[]
for i in range(len(data)):
    std.append(float(data[i][1]))
    measure.append(float(data[i][2]))
    yfited.append(float(data[i][3])) 
class MyChartView(QChartView):
    def __init__(self, parent = None):
        super(MyChartView, self).__init__(parent)
        self.setRenderHint(QPainter.Antialiasing)
        
        # self.chart().setTitle('单击与散点进行交互')
        self.line = QLineSeries()
        self.scatter = QScatterSeries()
        # self.scatter.setName('散点1')
        for i in range(len(std)):
            self.scatter.append(measure[i],std[i])
            self.line.append(measure[i],yfited[i])
        
        self.chart().addSeries(self.scatter)
        self.chart().addSeries(self.line)
        self.chart().createDefaultAxes()
        
        self.scatter.clicked.connect(self.handleClickedPoint)
        
    def handleClickedPoint(self, point):
        clickedPoint = point
        #从散点序列中发现最接近的点
        closest = QPointF(-sys.maxsize-1,  sys.maxsize)
        distance = sys.maxsize
        points = self.scatter.pointsVector()
        for currentPoint in points:
            dx = currentPoint.x() - clickedPoint.x()
            dy = currentPoint.y() - clickedPoint.y()
            currentDistance = math.sqrt(dx * dx + dy * dy)
            if currentDistance < distance:
                distance = currentDistance
                closest = currentPoint
        print(dir(closest),closest) 
        print(dir(self.scatter),self.scatter)  
        #从散点序列1中删除最近的点，并将其附加到散点序列2中
        # self.scatter.remove(closest)
        # self.scatter2.append(closest)
        
 
class DemoScatterInteractions(QMainWindow):
    def __init__(self, parent=None):
        super(DemoScatterInteractions, self).__init__(parent)   
        
         # 设置窗口标题
        self.setWindowTitle('实战 Qt for Python: 散点图交互演示')      
        # 设置窗口大小
        self.resize(480, 360)
        
        chartView = MyChartView(self)  
        self.setCentralWidget(chartView)
      
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = DemoScatterInteractions()
    window.show()
    sys.exit(app.exec())   